package bb.lanxing.fragment.routebook;

import androidx.annotation.NonNull;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import bb.lanxing.App;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.network.BiCiCallback;
import bb.lanxing.network.MyHttpClient;

public class CollectionRouteBookListFragment extends RouteBookListFragmentNew {

    @Override
    public void requestData() {
        requestCollectionList();
    }

    public void requestCollectionList() {
        MyHttpClient.requestCollectionLushuList(new BiCiCallback() {
            @Override
            public void onResponseString(@NonNull String string) {
                refreshComplete();
                ArrayList<RouteBook> arrayList = new ArrayList<>();
                try {
                    JSONObject obj = new JSONObject(string);
                    JSONArray jSONArray = obj.getJSONArray("rows");
                    for (int i = 0; i < jSONArray.length(); i++) {
                        RouteBook parseRouteBookByServer = RouteBook.parseLushuByServer(jSONArray.getJSONObject(i), true);
                        parseRouteBookByServer.setIsCollected(true);
                        arrayList.add(parseRouteBookByServer);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                refreshUIWithServer(arrayList, true);
            }

            @Override
            public void onResponseFail(String errMsg) {
                super.onResponseFail(errMsg);
                App.getContext().showMessage("请求失败！");
                refreshComplete();
            }
        });
    }
}